package cn.fan.whale.utils;

import cn.fan.whale.entity.po.DepartmentEntity;

import java.util.*;
import java.util.stream.Collectors;

public class TreeUtils {

    public static List<Integer> getChildNodeIdList(List<DepartmentEntity> sources, Integer deptId) {
        List<DepartmentEntity> departmentTree = list2Tree(sources);
        DepartmentEntity node = getNode(departmentTree, deptId);
        return getAllTreeNodeIdList(node);
    }

    // list转tree
    public static List<DepartmentEntity> list2Tree(List<DepartmentEntity> sources) {
        Map<Integer, List<DepartmentEntity>> parentIdChildrenMap = new HashMap<>();
        sources.forEach(source -> {
            List<DepartmentEntity> children = parentIdChildrenMap.getOrDefault(source.getParentId(), new ArrayList<>());
            children.add(source);
            parentIdChildrenMap.put(source.getParentId(), children);
        });
        sources.forEach(source -> source.setChildren(parentIdChildrenMap.get(source.getDeptId())));
        return sources.stream().filter(source -> source.getParentId().equals(0)).collect(Collectors.toList());
    }

    // 获取tree的指定节点
    public static DepartmentEntity getNode(List<DepartmentEntity> sources, Integer deptId) {
        DepartmentEntity result = null;
        if (sources != null && sources.size() > 0) {
            for (DepartmentEntity source : sources) {
                if (source.getDeptId().equals(deptId)) {
                    result = source;
                } else {
                    if (result == null) {
                        result = getNode(source.getChildren(), deptId);
                    }
                }
            }
        }
        return result;
    }

    // 获取当前节点下所有的子节点id
    public static List<Integer> getAllTreeNodeIdList(DepartmentEntity source) {
        List<Integer> result = new ArrayList<>();
        result.add(source.getDeptId());
        List<DepartmentEntity> children = source.getChildren();
        if (children != null && children.size() > 0) {
            for (DepartmentEntity item : children) {
                result.addAll(getAllTreeNodeIdList(item));
            }
        }
        return result;
    }


//    public static void main(String[] args) {
//        List<DepartmentEntity> sources = new ArrayList<>();
//        DepartmentEntity d = new DepartmentEntity();
//        d.setDeptId(1);
//        d.setParentId(0);
//        d.setDeptName("西安分公司");
//        sources.add(d);
//        DepartmentEntity d1 = new DepartmentEntity();
//        d1.setDeptId(2);
//        d1.setParentId(0);
//        d1.setDeptName("深圳分公司");
//        sources.add(d1);
//        DepartmentEntity d2 = new DepartmentEntity();
//        d2.setDeptId(3);
//        d2.setParentId(1);
//        d2.setDeptName("销售部门");
//        sources.add(d2);
//        DepartmentEntity d3 = new DepartmentEntity();
//        d3.setDeptId(4);
//        d3.setParentId(1);
//        d3.setDeptName("研发部门");
//        sources.add(d3);
//        DepartmentEntity d4 = new DepartmentEntity();
//        d4.setDeptId(5);
//        d4.setParentId(1);
//        d4.setDeptName("财务部门");
//        sources.add(d4);
//        DepartmentEntity d5 = new DepartmentEntity();
//        d5.setDeptId(6);
//        d5.setParentId(2);
//        d5.setDeptName("运维部门");
//        sources.add(d5);
//        DepartmentEntity d6 = new DepartmentEntity();
//        d6.setDeptId(7);
//        d6.setParentId(4);
//        d6.setDeptName("外包");
//        sources.add(d6);
//
//        List<DepartmentEntity> result = list2Tree(sources);
//        System.out.println("result: " + result);
//        DepartmentEntity departmentEntity = getNode(result, 4);
//        System.out.println("departmentEntity: " + departmentEntity.getDeptId());
//        List<Integer> nodeIds = getChildNodeIdList(sources, 4);
//        System.out.println("ids: " + nodeIds);
//
//    }
}
